package com.ejuornal.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.AticalEditor;
import com.ejuornal.bean.Check;
import com.ejuornal.bean.Editor;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.User;
import com.ejuornal.service.ArticalService;
import com.ejuornal.service.AticalEditorService;
import com.ejuornal.service.CheckService;
import com.ejuornal.service.EditorCommitService;
import com.ejuornal.service.MessageService;
import com.ejuornal.service.PeriodicalService;
import com.ejuornal.service.UserService;
import com.google.gson.Gson;

public class ArticalKJAction extends BaseAction<Artical> {

	private static final long serialVersionUID = 1L;
	private PeriodicalService periodicalService;
	private ArticalService articalService;
	private CheckService checkService;
	private AticalEditorService aticalEditorService;
	private Artical artical;
	private Check check;
	private List<Check> listChecks ;
	private UserService userService;
	private EditorCommitService editorCommitService;
	private Editor editor;
	private User  user;
	private Periodical periodical;
	private String value;
	private MessageService  messageService;
	private List<String> refDocList;//参考文献

	/**
	 * 编辑1
	 * 根据状态查找文章 status 1未审核、2退稿、3发表、4一审、5二审、6三审
	 * 
	 * @return
	 */
	public String checkByStatusEdit() {
		/*int  userIdEditor = getUserIdBySession();
		String  url = "artical/checkByStatusEdit";
		 pages = articalService.checkByStatusEditor((page-1)*10, url,userIdEditor, "1");*/
		// 此处按身份展示文章状态列表,status 是根据用户的身份来赋值
		int  userIdEditor = getUserIdBySession();
		String url = "artical/checkByStatusEdit";
		pages = articalService.checkByStatusEditor((page - 1) * 10, url, userIdEditor, "4");
		return SUCCESS;
	}

	public String findArticalByIdEdit() {
		artical = articalService.findById(artical.getArticalId());
		String refDoc = artical.getRefDoc();
		refDocList = splidRefDoc(refDoc);//参考文献
		
		periodical = periodicalService.findPeriodicalByArticalId(artical.getArticalId());
		user = userService.getUser(artical.getArticalId());
		
		if("2".equals(value)) {
			return "myArtical";
		}
		return SUCCESS;
	}
	
	/**
	 *根据空格分隔指定字符串
	 *@author lwf
	 * @param doc 指定字符串
	 * @return List<String>
	 */
	public List<String> splidRefDoc(String doc){
		List<String> docRefList = new ArrayList<String>(); 
		String docSplid[] = doc.split("\\s+|,");
		for (String string : docSplid) {
			docRefList.add(string);
		}
		return docRefList;
	}

	public String addCheckContentEdit() {
		
		
		 int  userIdEditor = getUserIdBySession();
		check.setArticalId(check.getArticalId());
		check.setCheckDate(new Date());
		 check.setUserId(userIdEditor);
		if (check.getStatus().equals("1")) {// 通过
			// 改变状态atical 和check中的status
			articalService.updateStatus("5", check.getArticalId());// 修改文章状态进入第二审
			checkService.add(check);
			
		} else if (check.getStatus().equals("2")) { 
			// status 2 直接退稿
			articalService.updateStatus("2", check.getArticalId());//
			checkService.add(check);
			// 给用户发送退稿邮件!!!!
			messageService.sendMessage(periodicalService, articalService, userService, messageService,check.getArticalId() , "2");
		}
		return SUCCESS;
	}
	/**
	 * 编委
	 * @return
	 */
	// status 1未审核、2退稿、3发表、4一审、5二审、6三审
	public String checkByStatusCommit() {
		int  userIdEditor = getUserIdBySession();
		String url = "artical/checkByStatusCommit";
		pages = articalService.checkByStatusCommit((page - 1) * 10, url, userIdEditor, "5");
		return SUCCESS;
	}

	public String findArticalByIdCommit() {
		artical = articalService.findById(artical.getArticalId());
		String refDoc = artical.getRefDoc();
		refDocList = splidRefDoc(refDoc);//参考文献
		
		periodical = periodicalService.findPeriodicalByArticalId(artical.getArticalId());
		user = userService.getUser(artical.getArticalId());
		return SUCCESS;
	}
/**************************/
	public String addCheckContentCommit() {
		int  userIdCommit = getUserIdBySession();
		check.setArticalId(check.getArticalId());
		check.setCheckDate(new Date());

	    check.setUserId(userIdCommit);
		checkService.add(check);
		// 根据 artical.articalId 通过 check得到评论数 来改变artical的status的状态
		// commitNum 编委数量     checkCNum   评论条数
		int commitNum = editorCommitService.getEditorCommits(check.getArticalId()).size();
		   int checkCNum = checkService.findCommitById(check.getArticalId()).size();
		if ((checkCNum-1) > (commitNum / 2 )) {
		   articalService.updateStatus("6", check.getArticalId());// 修改文章状态进入第3审
		}// 此处要控制编辑多次评论
		return SUCCESS;
	}
/***********************************************/
	/**
	 * 主编
	 * @return
	 */
	// status 1未审核、2退稿、3发表、4一审、5二审、6三审
	public String checkByStatusChief() {
		int  userIdEditor = getUserIdBySession();
		String url = "artical/checkByStatusChief";
		pages = articalService.checkByStatusChief((page - 1) * 10, url, userIdEditor, "5");
		return SUCCESS;
	}
	public String findArticalByIdChief() {
		artical = articalService.findById(artical.getArticalId());
		String refDoc = artical.getRefDoc();
		refDocList = splidRefDoc(refDoc);//参考文献
		
		periodical = periodicalService.findPeriodicalByArticalId(artical.getArticalId());
		user = userService.getUser(artical.getArticalId());
		listChecks = checkService.findById(artical.getArticalId());
		return SUCCESS;
	}
	public String addCheckContentChief() {
		int userIdEditor = getUserIdBySession();
		check.setArticalId(check.getArticalId());
		check.setCheckDate(new Date());
		check.setUserId(userIdEditor);
		//check.setUserId(4);
		if (check.getStatus().equals("1")) {
			// 改变状态atical 和check中的status
			articalService.updateStatus("7", check.getArticalId());// 修改文章状态进入第4审
			checkService.add(check);
			// 编辑editor 从第四审页面获取
		//	System.out.println(check.getArticalId()   + " * 3032             4   * " + user.getUserId());
			AticalEditor aticalEditor = new AticalEditor();
			aticalEditor.setArticalId(check.getArticalId());
			//aticalEditor.setUserId(user.getUserId());
			aticalEditor.setUserId(user.getUserId());
			aticalEditor.setCreateDate(new Date());
			aticalEditorService.addAticalEditor(aticalEditor);
			//发送审核通过通知
			messageService.sendMessage(periodicalService, articalService, userService, messageService,check.getArticalId() , "1");
		} else if (check.getStatus().equals("2")) {
			// status 2 直接退稿
			articalService.updateStatus("2", check.getArticalId());//
			checkService.add(check);
			// 给用户发送退稿邮件!!!!
			messageService.sendMessage(periodicalService, articalService, userService, messageService,check.getArticalId() , "2");
		}
		return SUCCESS;
	}
	/**
	 * 编辑4审
	 * @return
	 */
	// 第四审
	public String checkEditorLast() {
		int  userIdEditor = getUserIdBySession();
		String url = "artical/checkEditorLast";
		pages = articalService.checkEditorLast((page - 1) * 10, url, userIdEditor, "7");
		return SUCCESS;
	}

	public String checkByIdEditorLast() {
		artical = articalService.findById(artical.getArticalId());
		periodical = periodicalService.findPeriodicalByArticalId(artical.getArticalId());
		user = userService.getUser(artical.getArticalId());
		return SUCCESS;
	}

	public String updateEditorLast() {
		// 改变包括状态了  第四审！！！ 提交后  就发布了！！！
		artical.setPubDate(new Date());
		articalService.updateAtical(artical);
		return SUCCESS;
	}

	/**
	 * 获取某个期刊主编 对应的编辑列表
	 * 
	 * @return
	 */
	public String getListUsers() {
		
		int  userIdEditor = getUserIdBySession();
		List<User> listEditor = userService.getListUsersEdit(userIdEditor);
		Gson gson = new Gson();
		String listStr= gson.toJson(listEditor);
		HttpServletResponse res = ServletActionContext.getResponse();   
		res.reset();   
		res.setContentType("text/html;charset=utf-8");   
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.print(listStr);
			pw.flush();   
			pw.close();  
		} catch (IOException e) {
			e.printStackTrace();
		}   
		return null;
	}
	/**
	 * 根据文章的id得到该文章的推荐内容   checks
	 * @return
	 */
	public  String  findCommitById(){	
		 listChecks = checkService.findById(check.getArticalId());
			Gson gson = new Gson();
			String listStr= gson.toJson(listChecks);
			HttpServletResponse res = ServletActionContext.getResponse();   
			res.reset();   
			res.setContentType("text/html;charset=utf-8");   
			PrintWriter pw;
			try {
				pw = res.getWriter();
				pw.print(listStr);
				pw.flush();   
				pw.close();  
			} catch (IOException e) {
				e.printStackTrace();
			}   
			return null;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * 获取某个用户对象 服务类的方法
	 * @return
	 */
	public ArticalService getArticalService() {
		return articalService;
	}

	@Resource(name = "articalService")
	public void setArticalService(ArticalService articalService) {
		this.articalService = articalService;
	}

	public Artical getArtical() {
		return artical;
	}

	public void setArtical(Artical artical) {
		this.artical = artical;
	}

	public CheckService getCheckService() {
		return checkService;
	}

	@Resource(name = "checkService")
	public void setCheckService(CheckService checkService) {
		this.checkService = checkService;
	}

	public Check getCheck() {
		return check;
	}

	public void setCheck(Check check) {
		this.check = check;
	}

	public UserService getUserService() {
		return userService;
	}

	@Resource(name = "userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EditorCommitService getEditorCommitService() {
		return editorCommitService;
	}

	@Resource(name = "editorCommitService")
	public void setEditorCommitService(EditorCommitService editorCommitService) {
		this.editorCommitService = editorCommitService;
	}

	public AticalEditorService getAticalEditorService() {
		return aticalEditorService;
	}

	@Resource(name = "aticalEditorServiceImpl")
	public void setAticalEditorService(AticalEditorService aticalEditorService) {
		this.aticalEditorService = aticalEditorService;
	}

	public Editor getEditor() {
		return editor;
	}

	public void setEditor(Editor editor) {
		this.editor = editor;
	}

	public List<Check> getListChecks() {
		return listChecks;
	}

	public void setListChecks(List<Check> listChecks) {
		this.listChecks = listChecks;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Periodical getPeriodical() {
		return periodical;
	}

	public void setPeriodical(Periodical periodical) {
		this.periodical = periodical;
	}

	public PeriodicalService getPeriodicalService() {
		return periodicalService;
	}

	@Resource(name="periodicalService")
	public void setPeriodicalService(PeriodicalService periodicalService) {
		this.periodicalService = periodicalService;
	}

	public MessageService getMessageService() {
		return messageService;
	}
	
	@Resource(name="messageService")
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public List<String> getRefDocList() {
		return refDocList;
	}

	public void setRefDocList(List<String> refDocList) {
		this.refDocList = refDocList;
	}
}
